<template>
  <el-dialog
    :title="!dataForm.assetsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="姓名" prop="employeeName">
        <el-select  v-model="dataForm.employeeName" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

    <el-form-item label="KPI" prop="kpiScore">
      <el-input v-model="dataForm.kpiScore" placeholder=""></el-input>
    </el-form-item>
      <el-form-item label="奖惩情况" prop="des">
        <el-input v-model="dataForm.des" placeholder=""></el-input>
      </el-form-item>
    <el-form-item label="行为得分" prop="behaviorScore">
      <el-input v-model="dataForm.behaviorScore" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="能力得分" prop="abilityScore">
      <el-input v-model="dataForm.abilityScore" placeholder=""></el-input>
    </el-form-item>
      <el-form-item label="季度" prop="quarter">
        <el-select v-model="dataForm.quarter" placeholder="选择季度">
          <el-option label="第一季度" value="Q1"></el-option>
          <el-option label="第二季度" value="Q2"></el-option>
          <el-option label="第三季度" value="Q3"></el-option>
          <el-option label="第四季度" value="Q4"></el-option>
        </el-select>
      </el-form-item>
<!--    <el-form-item label="年份" prop="year">-->
<!--      <el-input v-model="dataForm.year" placeholder="" disabled></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        // url:'',
        visible: false,
        dataForm: {
          assetsId: 0,
          employeeName: '',
          kpiScore: '',
          behaviorScore: '',
          abilityScore: '',
          totalScore: '',
          assessmentDate: '',
          quarter: '',
          year: '',
          des: '',
        },
        options:[],
        dataRule: {
          employeeName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          kpiScore: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          behaviorScore: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          abilityScore: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          quarter: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        // this.url = this.$http.adornUrl(`/file/upload4?token=${this.$cookie.get('token')}`)
        this.getRealName()
        this.dataForm.assetsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.assetsId) {
            this.$http({
              url: this.$http.adornUrl(`/assessment/assessment/info/${this.dataForm.assetsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.employeeName = data.assessment.employeeName
                this.dataForm.kpiScore = data.assessment.kpiScore
                this.dataForm.behaviorScore = data.assessment.behaviorScore
                this.dataForm.abilityScore = data.assessment.abilityScore
                this.dataForm.totalScore = data.assessment.totalScore
                this.dataForm.assessmentDate = data.assessment.assessmentDate
                this.dataForm.quarter = data.assessment.quarter
                this.dataForm.year = data.assessment.year
                this.dataForm.des= data.assessment.des
              }
            })
          }
        })
      },
      //获得用户真实名字
      getRealName(){
        this.$http({
          url: this.$http.adornUrl(`/sys/user/list`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
           //console.log("dassadsd"+JSON.stringify(data.page.list))
            // 提取 userId 和 realName 并转换为 options 格式
            this.options = data.page.list.map(item => ({
              label: item.realName,
              value: item.userId
            }));
          }
        })

      },
      // 表单提交
      dataFormSubmit () {
        const currentDate = new Date();
        const currentYear = currentDate.getFullYear();
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/assessment/assessment/${!this.dataForm.assetsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'assetsId': this.dataForm.assetsId || undefined,
                'employeeName': this.dataForm.employeeName,
                'kpiScore': this.dataForm.kpiScore,
                'behaviorScore': this.dataForm.behaviorScore,
                'abilityScore': this.dataForm.abilityScore,
                'totalScore': this.dataForm.totalScore,
                'assessmentDate': new Date(),
                'quarter': this.dataForm.quarter,
                'year': currentYear,
                'des': this.dataForm.des
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
